package com.ruoyi.subject.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.subject.domain.QbSubWrong;
import com.ruoyi.subject.service.IQbSubWrongService;
import com.ruoyi.subject.util.QbSubjectUtil;
import com.ruoyi.user.service.IQbUserWxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 我的错题Controller
 *
 * @author lzhj
 * @date 2021-01-27
 */
@Api(tags = "我的错题")
@RestController
@RequestMapping("/qb/sub/wrong")
public class QbSubWrongController extends BaseController {
    @Autowired
    private IQbSubWrongService qbSubWrongService;

    @Autowired
    private IQbUserWxService qbUserWxService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询我的错题列表
     */
    @ApiOperation(value = "查询我的错题列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "userId", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "openid", dataType = "string", paramType = "query"),
    })
    @PostMapping("/list")
    public AjaxResult list(long userId, String openId, String token) {
        //验证用户信息
        if (!qbUserWxService.validataUserInfo(userId, openId)) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        List<QbSubWrong> list = qbSubWrongService.selectQbSubWrongListByUserId(userId);
        return AjaxResult.success("查询成功", list);
    }


    /**
     * 新增我的错题
     */
    @ApiOperation(value = "新增我的错题,新增一条数据", hidden = true)
    @Log(title = "我的错题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbSubWrong qbSubWrong) {
        //验证用户信息
        if (!qbUserWxService.validataUserInfo(qbSubWrong.getUserId(), qbSubWrong.getOpenId())) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        //判断是否存在改错题
        if (qbSubWrongService.selectQbSubWrongBy(qbSubWrong)) {
            return AjaxResult.success("错题已存在");
        }
        return toAjax(qbSubWrongService.insertQbSubWrong(qbSubWrong));
    }

    /**
     * 修改我的错题
     */
//    @Log(title = "我的错题", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody QbSubWrong qbSubWrong) {
//        return toAjax(qbSubWrongService.updateQbSubWrong(qbSubWrong));
//    }

    /**
     * 删除我的错题
     */
    @ApiOperation(value = "删除我的错题，根据错题id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "userId", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "openId", value = "openid", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ids", value = "错题id", dataType = "long", paramType = "query"),
    })
    @Log(title = "我的错题", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(long userId, String openId, @RequestParam(value = "ids") Long[] ids) {
        if (!qbUserWxService.validataUserInfo(userId, openId)) {
            return QbSubjectUtil.getWxValidataFailMag();
        }
        return toAjax(qbSubWrongService.deleteQbSubWrongByIds(ids));
    }
}
